package com.swpu.service_remind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_remind.entity.Medicine;
import com.swpu.service_remind.entity.MedicineRemind;
import com.swpu.service_remind.entity.vo.MedicineRemindVo;
import com.swpu.service_remind.mapper.MedicineRemindMapper;
import com.swpu.service_remind.service.MedicineRemindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swpu.service_remind.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-03-13
 */
@Service
public class MedicineRemindServiceImpl extends ServiceImpl<MedicineRemindMapper, MedicineRemind> implements MedicineRemindService {

    @Autowired
    private MedicineService medicineService;

    @Override
    public void saveMedicineRemindSetting(String openid, MedicineRemindVo medicineRemindVo) {
        MedicineRemind medicineRemind = medicineRemindVo.getMedicineRemind();
        medicineRemind.setUserId(openid);
        this.save(medicineRemind);
        String medicineRemindId = medicineRemind.getId();
        List<Medicine> medicines = medicineRemindVo.getMedicines();
        for (Medicine medicine : medicines) {
            medicine.setMedicineRemindId(medicineRemindId);
        }
        medicineService.saveBatch(medicines);

    }

    @Override
    public List<MedicineRemindVo> getMedicineRemindData(String openid) {
        LambdaQueryWrapper<MedicineRemind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicineRemind::getUserId,openid);
        List<MedicineRemind> medicineReminds = this.list(queryWrapper);
        ArrayList<MedicineRemindVo> medicineRemindVoList = new ArrayList<>();
        LambdaQueryWrapper<Medicine> medicineLambdaQueryWrapper=null;
        for (MedicineRemind medicineRemind : medicineReminds) {
            MedicineRemindVo medicineRemindVo = new MedicineRemindVo();
            medicineRemindVo.setMedicineRemind(medicineRemind);
            medicineLambdaQueryWrapper = new LambdaQueryWrapper<>();
            medicineLambdaQueryWrapper.eq(Medicine::getMedicineRemindId,medicineRemind.getId());
            List<Medicine> medicineList = medicineService.list(medicineLambdaQueryWrapper);
            medicineRemindVo.setMedicines(medicineList);
            medicineRemindVoList.add(medicineRemindVo);
        }
        return  medicineRemindVoList;
    }

    @Override
    public MedicineRemindVo getMedicineRemindById(String openid, String id) {
        LambdaQueryWrapper<Medicine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Medicine::getMedicineRemindId,id);
        List<Medicine> medicineList = medicineService.list(queryWrapper);
        MedicineRemind medicineRemind = this.getById(id);
        MedicineRemindVo medicineRemindVo = new MedicineRemindVo();
        medicineRemindVo.setMedicineRemind(medicineRemind);
        medicineRemindVo.setMedicines(medicineList);
        return medicineRemindVo;
    }

    @Override
    public void updateMedicineRemindById(String openid, MedicineRemindVo medicineRemindVo) {
        boolean update = this.updateById(medicineRemindVo.getMedicineRemind());
        if(!update) throw new RuntimeException("更新用药提醒失败");
        LambdaQueryWrapper<Medicine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Medicine::getMedicineRemindId,medicineRemindVo.getMedicineRemind().getId());
        medicineService.remove(queryWrapper);
        List<Medicine> medicines = medicineRemindVo.getMedicines();
        for (Medicine medicine : medicines) {
            medicine.setMedicineRemindId(medicineRemindVo.getMedicineRemind().getId());
        }
        medicineService.saveBatch(medicines);
    }

    @Override
    public void deleteMedicineRemindById(String id) {
        this.removeById(id);
        LambdaQueryWrapper<Medicine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Medicine::getMedicineRemindId,id);
        medicineService.remove(queryWrapper);
    }

    @Override
    public List<Medicine> getMedicinesById(String medicineRemindId) {
        LambdaQueryWrapper<Medicine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Medicine::getMedicineRemindId,medicineRemindId);
        return medicineService.list(queryWrapper);
    }
}
